﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace JSIF_WCFServer.Server
{
    /// <summary>
    /// A WCF Service host wrapper to facilitate multiple services running together to simulate a web site
    /// </summary>
    public class WebSiteHost
    {
        /// <summary>
        /// Adds a service endpoint, removes the "this is a service" warning, and configures it with the WebHttp behaviour
        /// </summary>
        /// <param name="implementation"></param>
        /// <param name="Interface"></param>
        /// <param name="binding"></param>
        /// <param name="address"></param>
        public void AddEndpoint(Object implementation, Type Interface, Binding binding, Uri address)
        {
            ServiceHost host = new ServiceHost(implementation, address);
            host.Description.Behaviors.Remove(typeof(ServiceDebugBehavior));
            
            ServiceEndpoint endpoint = host.AddServiceEndpoint(Interface, binding, address);
            endpoint.Behaviors.Add(new WebHttpBehavior());

            _hosts.Add(host);
        }

        /// <summary>
        /// Loops through each endpoint and makes sure it is open
        /// </summary>
        public void Open()
        {
            foreach (ServiceHost host in _hosts)
            {
                host.Open();
            }
        }

        /// <summary>
        /// Loops through each endpoint and closes them
        /// </summary>
        public void Close()
        {
            foreach (ServiceHost host in _hosts)
            {
                host.Close();
            }
        }

        private List<ServiceHost> _hosts = new List<ServiceHost>();
    }
}
